//
//  MyIntroPageControl.m
//  workpai
//
//  Created by zack on 15/9/22.
//  Copyright (c) 2015年 workorz. All rights reserved.
//

#import "MyIntroPageControl.h"

@interface MyIntroPageControl ()
- (void)updateDots;
@end

@implementation MyIntroPageControl


- (void)setImagePageStateNormal:(UIImage *)image {
    _imagePageStateNormal = image;
    [self updateDots];
}

- (void)setImagePageStateHighlighted:(UIImage *)image {
    _imagePageStateHighlighted = image;
    [self updateDots];
}

- (void)endTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event {
    [super endTrackingWithTouch:touch withEvent:event];
    [self updateDots];
}

-(void)updateDots {
    if (self.imagePageStateNormal || self.imagePageStateHighlighted) {
        for (int i = 0; i < [self.subviews count]; i++) {
            UIImageView * dot = [self imageViewForSubview:[self.subviews objectAtIndex:i]];
            dot.image = self.currentPage == i ? self.imagePageStateNormal : self.imagePageStateHighlighted;
        }
    }
}

- (UIImageView *)imageViewForSubview:(UIView *)view {
    UIImageView *dot = nil;
    if ([view isKindOfClass: [UIView class]]) {
        for (UIView* subview in view.subviews) {
            if ([subview isKindOfClass:[UIImageView class]]) {
                dot = (UIImageView *)subview;
                break;
            }
        }
        if (dot == nil) {
            dot = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, view.frame.size.width, view.frame.size.height)];
            [view addSubview:dot];
        }
    }
    else {
        dot = (UIImageView *)view;
    }
    return dot;
}


- (void)setCurrentPage:(NSInteger)currentPage {
    [super setCurrentPage:currentPage];
    [self updateDots];
}

- (void)dealloc {
    self.imagePageStateNormal = nil;
    self.imagePageStateHighlighted = nil;
}

@end
